package com.datetest;

import org.bouncycastle.util.test.Test;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeTest {

    public static void main(String[] args) {
        test1();
    }

    //时间戳转换为LocalDateTime
    public static void test1(){
        DateTimeFormatter pattern = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

        long start = 1721039059232L;
        long end = 1721097300763L;
        // 将时间戳转换为Instant对象 毫秒 也可以是秒
        Instant instant = Instant.ofEpochMilli(start);
        // 将Instant对象转换为LocalDateTime对象，使用系统默认时区
        LocalDateTime startTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

        String format = startTime.format(pattern);
        System.out.println(format);
        Instant instant2 = Instant.ofEpochMilli(end);
        // 将Instant对象转换为LocalDateTime对象，使用系统默认时区
        LocalDateTime endTime = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault());

        Duration duration = Duration.between(startTime, endTime);
        // 获取相隔的天数、小时、分钟和秒
        long days = duration.toDays();
        long hours = duration.toHours() % 24;
        long minutes = duration.toMinutes() % 60;
        long seconds = duration.getSeconds() % 60;

        System.out.println("相隔时间: " + days + "天 " + hours + "小时 " + minutes + "分钟 " + seconds + "秒");
    }
}
